package com.cdxy.thrid;

import com.cdxy.biz.process.IRechargeBizProcess;
import com.cdxy.model.dto.base.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author: tangy
 * @Date: 2020/4/7 16:27
 */
@RestController
@RequestMapping("/third")
@Api(tags = "第三方通知接口")
@Slf4j
public class AliThirdController extends BaseController {

    @Autowired
    private IRechargeBizProcess rechargeBizProcess;

    @PostMapping("/aliPayNotifyScene")
    @ApiOperation(value = "支付宝景点充值异步通知", notes = "支付宝充值异步通知")
    public String aliPayNotifyScene(HttpServletRequest request) {
        try {
            // 获取支付宝POST过来反馈信息
            Map<String, String> params = new HashMap();
            Map requestParams = request.getParameterMap();
            for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                // 乱码解决，这段代码在出现乱码时使用。
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
                params.put(name, valueStr);
            }
            log.info("notifyScene:param | {}", params);
            return rechargeBizProcess.aliPayNotifyScene(params);
        } catch (Exception e) {
            log.error("支付宝充值异步通知回調異常", e);
            return "FAIL";
        }
    }


    @PostMapping("/aliPayNotifyHotel")
    @ApiOperation(value = "支付宝酒店充值异步通知", notes = "支付宝充值异步通知")
    public String aliPayNotifyHotel(HttpServletRequest request) {
        try {
            // 获取支付宝POST过来反馈信息
            Map<String, String> params = new HashMap();
            Map requestParams = request.getParameterMap();
            for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                // 乱码解决，这段代码在出现乱码时使用。
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
                params.put(name, valueStr);
            }

            log.info("notifyHotel:param | {}", params);
            return rechargeBizProcess.aliPayNotifyHotel(params);
        } catch (Exception e) {
            log.error("支付宝充值异步通知回調異常", e);
            return "FAIL";
        }
    }
}
